package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    AliOssUtil aliOssUtil;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        try {
            log.info("wenjian{}",imgFile);
            String originalFilename = imgFile.getOriginalFilename();//原始文件名
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectName = UUID.randomUUID() + extension;

            //请求路径
            String filePath = aliOssUtil.upload(imgFile.getBytes(), objectName);//ObjectName就是上传上去的文件名用UUID

            //试用redis的Set集合缓存图片(文件路径)，已上传就开始缓存，注：添加成功时再保存到另一份Set用于做差集
            redisTemplate.opsForSet().add("AliOssUtilFirst",filePath);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,filePath);
                    //Result.success(filePath);
        } catch (IOException e) {
            log.info("文件上传失败");
        }
        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }

    @ApiOperation("新增检查组")
    @PostMapping("/add")
    public Result setmealAdd(@RequestBody Setmeal setmeal, @RequestParam List<Integer> checkgroupIds) {
        setmealService.addSetmeal(setmeal, checkgroupIds);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    @ApiOperation("检查组分页查询")
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult=setmealService.pageQuery(queryPageBean);
        return pageResult;
    }

    @ApiOperation("根据id查询套餐")
    @GetMapping("/findById")
    public Result findById(Integer id){
        Setmeal setmeal=setmealService.getByid(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,setmeal);
    }

    @ApiOperation("套餐编辑功能")
    @PostMapping("/edit")
    public Result editGroup(@RequestBody Setmeal setmeal,@RequestParam List<Integer> checkGroupIds){
        setmealService.edit(setmeal,checkGroupIds);
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @ApiOperation("套餐删除功能")
    @PostMapping("/delete")
    public Result editGroup(Long id){

        return new Result(true,"套餐删除成功");
    }
}
